﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    //滚动条

    public class ScrollBar : Label
    {
        private int vNow = 0;   //当前值
        public int vMax = 0;    //最大值
        public int vMin = 0;    //最小值
        public int sP = 0;      //起点位置
        public int eP = 0;      //终点位置
        private int sL;         //滑块起点坐标
        private int eL;         //滑块终点坐标
        private int mL;         //滑块长度
        public bool HV = false; //HV==true为横着   false为竖着
        public bool Stealth = false;


        public Form1 form1;
        public Panel panel;

        private void RefreshTmp()
        {
            float tmp = vMax - vMin;
            
            mL = (int)((eP - sP)*(eP - sP) / tmp);

            if (mL > tmp)
            {
                mL = (int)tmp;
                this.Visible = false;
                panel.Top = 0;
                return;
            }
            else
            {
                if (mL < 20) mL = 20;
                this.Visible = true;
            }

            sL = sP;
            eL = eP - mL;

            float proportion = (float)(eL - sL) / (vMax - vMin - (eP - sP));

            if (HV)
            {
                Left = (int)(sL + vNow * proportion);
                Width = mL;
            }
            else
            {
                Top = (int)(sL + vNow * proportion);
                Height = mL;
            }

            //now_value
        }

        public int Now_value {
            get {
                return vNow; 
            }
            set
            {
                vNow = value;
                RefreshTmp();
            }
        }

        public ScrollBar()
        {
            MouseDown       += new MouseEventHandler(ScrollBar_MouseDown);
            MouseUp         += new MouseEventHandler(ScrollBar_MouseUp);
            MouseMove       += new MouseEventHandler(ScrollBar_MouseMove);
            MouseLeave      += new EventHandler(ScrollBar_MouseLeave);
            MouseEnter      += new EventHandler(ScrollBar_MouseEnter);
        }

        private int StealthMode = 0;
        public void Time_Stealth()
        {
            if (!Stealth)
            {
                BackColor = Parent.BackColor;
                return;
            }

            Point panelPoint = Parent.PointToClient(Control.MousePosition); //鼠标相对于panel3左上角的坐标

            if (panelPoint.X > Parent.Left &&
                panelPoint.Y > Parent.Top &&
                panelPoint.X < (Parent.Left + Parent.Width) &&
                panelPoint.Y < (Parent.Top + Parent.Height))
            {
                if (StealthMode < 10)
                {
                    StealthMode = 10;
                }
            }
            else
            {
                if (StealthMode >= 10 && !ScrollBarMouseDown)
                {
                    StealthMode = 9;
                }
            }
            switch (StealthMode)
            {
                case 0:
                    BackColor = Parent.BackColor;
                    break;
                case 10:
                    BackColor = Color.FromArgb(15, 19, 25);
                    break;
                case 11:
                    BackColor = Color.FromArgb(20, 24, 31);
                    break;
                case 12:
                    BackColor = Color.FromArgb(38, 44, 51);
                    break;
                default:
                    if (StealthMode < 20 && StealthMode > 0)
                    {
                        float PLR, PLG, PLB;
                        PLR = Parent.BackColor.R;
                        PLG = Parent.BackColor.G;
                        PLB = Parent.BackColor.B;
                        PLR += (15 - PLR) / 10 * StealthMode;
                        PLG += (19 - PLG) / 10 * StealthMode;
                        PLB += (25 - PLB) / 10 * StealthMode;
                        BackColor = Color.FromArgb((int)PLR, (int)PLG, (int)PLB);
                        StealthMode--;
                    }
                    else
                    {
                        StealthMode = 0;
                    }
                    break;
            }
        }

        private bool ScrollBarMouseDown = false;
        private Point panelPointOld;
        private Point LocationOld;
        private void ScrollBar_MouseDown(object sender, MouseEventArgs e)  //按下
        {
            
            ScrollBarMouseDown = true;
            Panel panel = this.Parent as Panel;
            panelPointOld = panel.PointToClient(Control.MousePosition); //鼠标相对于panel3左上角的坐标
            LocationOld = Location;
            StealthMode = 12;
            BackColor = Color.FromArgb(38, 44, 51);
        }

        private void ScrollBar_MouseMove(object sender, MouseEventArgs e)  //移动
        {
            if (ScrollBarMouseDown)
            {
                Panel panel = this.Parent as Panel;
                Point panelPoint = panel.PointToClient(Control.MousePosition); //鼠标相对于panel3左上角的坐标
                float proportion = (float)(eL - sL) / (vMax - vMin - (eP - sP));
                if (HV)
                {
                    int tmp = panelPoint.X - panelPointOld.X + LocationOld.X;
                    if (tmp > eL) tmp = eL;
                    if (tmp < sL) tmp = sL;
                    Left = tmp;
                    vNow = (int)((Left - sL) / proportion);
                    this.panel.Left = -vNow;
                }
                else
                {
                    int tmp = panelPoint.Y - panelPointOld.Y + LocationOld.Y;
                    if (tmp > eL) tmp = eL;
                    if (tmp < sL) tmp = sL;
                    Top = tmp;
                    vNow = (int)((Top - sL) / proportion);
                    this.panel.Top = -vNow;
                }
                
            }
        }

        private void ScrollBar_MouseUp(object sender, MouseEventArgs e)  //松开
        {
            ScrollBarMouseDown = false;
            if (!Stealth)
            {
                BackColor = Parent.BackColor;
                return;
            }
            StealthMode = 9;
            BackColor = Color.FromArgb(15, 19, 25);
        }

        private void ScrollBar_MouseEnter(object sender, EventArgs e)  //进入可视部分
        {
            if (!Stealth)
            {
                BackColor = Parent.BackColor;
                return;
            }
            if (!ScrollBarMouseDown)
            {
                StealthMode = 11;
                BackColor = Color.FromArgb(20, 24, 31);
            }
        }

        private void ScrollBar_MouseLeave(object sender, EventArgs e)  //离开可视部分
        {
            if (!Stealth)
            {
                BackColor = Parent.BackColor;
                return;
            }
            if (!ScrollBarMouseDown)
            {
                StealthMode = 10;
                BackColor = Color.FromArgb(15, 19, 25);
            }
        }
    }
}
